package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.traval.biz.DiaryBiz;
import com.etc.traval.biz.DiarycomBiz;
import com.etc.traval.biz.PlaceBiz;
import com.etc.traval.bizimpl.DiaryBizImpl;
import com.etc.traval.bizimpl.DiarycomBizImpl;
import com.etc.traval.bizimpl.PlaceBizImpl;
import com.etc.traval.dao.PlaceDao;
import com.etc.traval.daoimpl.PlaceDaoImpl;
import com.etc.traval.util.MyUtil;
import com.etc.travel.entity.Diary;
import com.etc.travel.entity.Diarycom;
import com.etc.travel.entity.Place;
import com.etc.travel.entity.User;
import com.google.gson.Gson;

public class DiaryDetailServlet extends HttpServlet {

	/**
		 * Constructor of the object.
		 */
	public DiaryDetailServlet() {
		super();
	}

	/**
		 * Destruction of the servlet. <br>
		 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		DiarycomBiz diarycomBiz = new DiarycomBizImpl();
		DiaryBiz diaryBiz = new DiaryBizImpl();
		PlaceBiz placeBiz = new PlaceBizImpl();
		String flag = null;
		if(request.getParameter("flag")!=null){
			flag = request.getParameter("flag");
		}
		if(flag.equals("diarydetail")){
			int size=5;
		    int pageIndex=1;
		    int diaryid = Integer.valueOf(request.getParameter("diaryid"));
		    Diary diary = diaryBiz.queryDiaryById(diaryid);
		    int diarycomnum = diarycomBiz.queryCountByDiary(diary);
		    diary.setDiaryComNum(diarycomnum);
		    //该游记的评论总数
		    int count=diarycomBiz.queryCountByDiary(diary);
		    int pageCount=(count%size==0)?(count/size):(count/size+1);		   	
		    //获取对应游记的评论
		   List<Diarycom> list= diarycomBiz.queryDiarycomByDiaryId(diaryid,size, pageIndex);
		    request.setAttribute("mylist", list);
		    request.setAttribute("diary", diary);
		    request.setAttribute("diarycomcount", count);
			request.setAttribute("pageCount",pageCount);
			request.setAttribute("pageIndex",pageIndex);			
			request.getRequestDispatcher("diarydetail.jsp").forward(request, response);
		    
		    
		}else if (flag.equals("getotherdiarycompage")) {
			int diaryid = Integer.valueOf(request.getParameter("diaryid"));
			int otherpage =Integer.valueOf(request.getParameter("otherpage"));
			int size=5;
		    Diary diary = diaryBiz.queryDiaryById(diaryid);
		    int diarycomnum = diarycomBiz.queryCountByDiary(diary);
		    diary.setDiaryComNum(diarycomnum);

			List<Diarycom> list= diarycomBiz.queryDiarycomByDiaryId(diaryid,size,otherpage);
			 Gson gson=new Gson();
			 String jsonString= gson.toJson(list);
			 response.setContentType("text/html;charset=utf-8");;
			 PrintWriter out = response.getWriter();
			 out.print(jsonString);
			 out.flush();
			 out.close(); 
		}else if (flag.equals("whatareyousaying")) {
			String content=request.getParameter("getsaysomething");
			int diaryid =Integer.valueOf(request.getParameter("diaryid"));
			HttpSession session =request.getSession();
			User user = (User) session.getAttribute("user");
			Diarycom diarycom = new Diarycom();
			diarycom.setDiarycom_content(content);
			diarycom.setDiarycom_user(user);
			MyUtil myUtil = new MyUtil();
			Timestamp date = myUtil.getWebsiteDatetime();      		  
			diarycom.setDiarycom_time(date);
			Diary diary=diaryBiz.queryDiaryById(diaryid);
			diarycom.setDiarycom_diary(diary);
			if(diarycomBiz.addDiarycom(diarycom)){
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				out.print("<script>alert('留言成功'); location.href='DiaryDetailServlet?flag=diarydetail&diaryid="+diaryid+"';</script>");
				out.flush();
				out.close();
			}else {
				response.setContentType("text/html;charset=utf-8");
				PrintWriter out = response.getWriter();
				//添加失败
				out.print("<script>alert('字数过多'); history.go(-1);</script>");
				out.flush();
				out.close();
			}	
		}else if (flag.equals("inadddiarygetplace")) {			
			List<Place> list=placeBiz.queryAllPlace(1,99999999);
			Gson gson=new Gson();
			String jsonString= gson.toJson(list);
		    response.setContentType("text/html;charset=utf-8");;
		    PrintWriter out = response.getWriter();
		    out.print(jsonString);
			out.flush();
			out.close(); 
		}
		
		
		
		
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	/**
		 * Initialization of the servlet. <br>
		 *
		 * @throws ServletException if an error occurs
		 */
	public void init() throws ServletException {
		// Put your code here
	}

}
